//package com.example.idachuappone.index.item;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import org.json.JSONArray;
//import org.json.JSONException;
//import org.json.JSONObject;
//
//import android.app.Activity;
//import android.content.DialogInterface;
//import android.content.DialogInterface.OnCancelListener;
//import android.content.Intent;
//import android.os.Bundle;
//import android.os.IBinder;
//import android.text.Editable;
//import android.text.TextUtils;
//import android.text.TextWatcher;
//import android.util.Log;
//import android.view.MotionEvent;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.view.inputmethod.InputMethodManager;
//import android.widget.AdapterView;
//import android.widget.AdapterView.OnItemClickListener;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.ImageView;
//import android.widget.LinearLayout;
//import android.widget.ListView;
//import android.widget.ProgressBar;
//
//import com.example.idachuappone.R;
//import com.example.idachuappone.index.adapter.AddressWebAdapter;
//import com.example.idachuappone.person.method.AddressMethod;
//import com.example.idachuappone.person.method.AddressMethod.SetAddress;
//import com.example.idachuappone.promptly.adapter.AddressAdapter;
//import com.example.idachuappone.promptly.entity.Address;
//import com.example.idachuappone.promptly.entity.UserInfo;
//import com.example.idachuappone.utils.CheckUtil;
//import com.example.idachuappone.utils.Constant;
//import com.example.idachuappone.utils.CusProSmallBgDialog;
//import com.example.idachuappone.utils.MainToast;
//import com.example.idachuappone.utils.NetUtil;
//import com.lidroid.xutils.HttpUtils;
//import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
//import com.lidroid.xutils.exception.HttpException;
//import com.lidroid.xutils.http.ResponseInfo;
//import com.lidroid.xutils.http.callback.RequestCallBack;
//import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
//
//public class AddressSerchIndexActivity extends Activity implements OnClickListener, OnItemClickListener,
//		RecognizerDialogListener {
//	private AddressAdapter addressAdapter;
//	private AddressWebAdapter addressWebAdapter;
//	private Button btn_address_back;
//	private CusProSmallBgDialog cusProDialog;
//	private EditText et_address;
//	private ImageView img_voice1;
//	private ImageView img_voice_clear;
//	private boolean isHistory;
//	private List<Address> list;
//	private LinearLayout ll_back;
//	private ListView lv;
//	private TextWatcher myTextWatcher;
//	private String name;
//	private String path1;
//	private String path2;
//	private ProgressBar pb_zairu;
//	private RecognizerDialog recognizerDialog;
//	private StringBuffer sb;
//	private String temp;
//	private UserInfo userInfo;
//
//	public AddressSerchIndexActivity() {
//		this.path1 = "http://api.map.baidu.com/place/v2/suggestion?query=";
//		this.path2 = "&region=131&output=json&ak=C1bdec719d928d8581906fc068c35bee";
//		this.isHistory = true;
//		this.myTextWatcher = new TextWatcher() {
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before, int count) {
//				AddressSerchIndexActivity.this.img_voice1
//						.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
//				AddressSerchIndexActivity.this.img_voice_clear
//						.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
//				AddressSerchIndexActivity.this.name = AddressSerchIndexActivity.this.et_address.getText().toString()
//						.trim().replace(" ", "");
//				if (AddressSerchIndexActivity.this.name == null || AddressSerchIndexActivity.this.name.length() <= 0) {
//					AddressSerchIndexActivity.this.isHistory = true;
//					AddressSerchIndexActivity.this.list = null;
//					AddressSerchIndexActivity.this.initAdapter();
//					AddressSerchIndexActivity.this.img_voice_clear
//							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
//					AddressSerchIndexActivity.this.img_voice1.setVisibility(0);
//					return;
//				}
//				AddressSerchIndexActivity.this.isHistory = false;
//				AddressSerchIndexActivity.this.pb_zairu.setVisibility(0);
//				new HttpUtils().send(
//						HttpMethod.GET,
//						new StringBuilder(String.valueOf(AddressSerchIndexActivity.this.path1))
//								.append(AddressSerchIndexActivity.this.name)
//								.append(AddressSerchIndexActivity.this.path2).toString(),
//						new RequestCallBack<String>() {
//							@Override
//							public void onFailure(HttpException arg0, String arg1) {
//								MainToast.show(AddressSerchIndexActivity.this, AddressSerchIndexActivity.this
//										.getResources().getString(R.string.netFail), 1);
//							}
//
//							@Override
//							public void onSuccess(ResponseInfo<String> responseInfo) {
//								if (responseInfo.result != null && responseInfo.result.length() > 0) {
//									AddressSerchIndexActivity.this.pb_zairu
//											.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
//									AddressSerchIndexActivity.this.img_voice_clear.setVisibility(0);
//									AddressSerchIndexActivity.this.list = new ArrayList();
//									try {
//										JSONArray jsonArray = new JSONObject(responseInfo.result)
//												.getJSONArray("result");
//										int index = jsonArray.length();
//										if (jsonArray.length() >= 10) {
//											index = ax.iB;
//										}
//										for (int i = 0; i < index; i++) {
//											Address postion = new Address().parseJson(jsonArray.getJSONObject(i));
//											if (postion != null) {
//												AddressSerchIndexActivity.this.list.add(postion);
//											}
//										}
//									} catch (Exception e) {
//										e.printStackTrace();
//									}
//									if (AddressSerchIndexActivity.this.list == null
//											|| AddressSerchIndexActivity.this.list.size() <= 0) {
//										AddressSerchIndexActivity.this.list = null;
//										AddressSerchIndexActivity.this.addressAdapter.setIndex("");
//										AddressSerchIndexActivity.this.initAdapter();
//										return;
//									}
//									AddressSerchIndexActivity.this.addressAdapter
//											.setIndex(AddressSerchIndexActivity.this.et_address.getText().toString());
//									AddressSerchIndexActivity.this.initAdapter();
//								}
//							}
//						});
//			}
//
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//			}
//
//			@Override
//			public void afterTextChanged(Editable s) {
//			}
//		};
//	}
//
//	@Override
//	protected void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_address_serch_index);
//		this.cusProDialog = new CusProSmallBgDialog(this);
//		this.cusProDialog.setOnCancelListener(new OnCancelListener() {
//			@Override
//			public void onCancel(DialogInterface dialog) {
//				AddressSerchIndexActivity.this.cusProDialog.dismiss();
//			}
//		});
//		initView();
//		checkNet();
//	}
//
//	@Override
//	protected void onResume() {
//		super.onResume();
//		this.isHistory = true;
//	}
//
//	private void checkNet() {
//		if (CheckUtil.checkNet(getApplicationContext())) {
//			initData();
//		} else {
//			MainToast.show(this, "\u5f53\u524d\u7f51\u7edc\u4e0d\u53ef\u7528", 0);
//		}
//	}
//
//	private void initView() {
//		this.pb_zairu = (ProgressBar) findViewById(R.id.pb_zairu);
//		this.img_voice1 = (ImageView) findViewById(R.id.img_voice1);
//		this.img_voice1.setOnClickListener(this);
//		this.img_voice_clear = (ImageView) findViewById(R.id.img_voice_clear);
//		this.img_voice_clear.setOnClickListener(this);
//		this.btn_address_back = (Button) findViewById(R.id.btn_address_back);
//		this.btn_address_back.setOnClickListener(this);
//		this.ll_back = (LinearLayout) findViewById(R.id.ll_back);
//		this.ll_back.setOnClickListener(this);
//		this.et_address = (EditText) findViewById(R.id.et_address_name);
//		this.et_address.addTextChangedListener(this.myTextWatcher);
//		this.lv = (ListView) findViewById(R.id.lv_address);
//		this.lv.setSelector(R.color.trans);
//		this.addressAdapter = new AddressAdapter(this);
//		this.addressWebAdapter = new AddressWebAdapter(this);
//		this.lv.setOnItemClickListener(this);
//		this.recognizerDialog = new RecognizerDialog(this, "appid=52aec554");
//		this.recognizerDialog.setListener(this);
//	}
//
//	private void initData() {
//		this.cusProDialog.setMsg("\u8f7d\u5165\u4e2d...");
//		this.cusProDialog.show();
//		NetUtil.get(this, Constant.USER_INFO, 100, new RequestCallBack<String>() {
//			@Override
//			public void onSuccess(ResponseInfo<String> responseInfo) {
//				try {
//					AddressSerchIndexActivity.this.cusProDialog.dismiss();
//					JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
//					Log.e("ffff", NetUtil.unescapeUnicode(responseInfo.result));
//					if (jsonObject.getInt("code") == 10000) {
//						AddressSerchIndexActivity.this.userInfo = new UserInfo();
//						AddressSerchIndexActivity.this.userInfo.parseJson(jsonObject.getJSONObject("data"));
//						if (AddressSerchIndexActivity.this.userInfo.getAddresses() != null
//								&& AddressSerchIndexActivity.this.userInfo.getAddresses().size() > 0) {
//							AddressSerchIndexActivity.this.initAdapter();
//						}
//					} else if (jsonObject.getInt("code") == 300) {
//						MainToast.show(AddressSerchIndexActivity.this, jsonObject.getString("error"), 0);
//					}
//				} catch (JSONException e) {
//					e.printStackTrace();
//				}
//			}
//
//			@Override
//			public void onFailure(HttpException arg0, String arg1) {
//				AddressSerchIndexActivity.this.cusProDialog.dismiss();
//				MainToast.show(AddressSerchIndexActivity.this,
//						AddressSerchIndexActivity.this.getResources().getString(R.string.netFail), 0);
//			}
//		});
//	}
//
//	private void initAdapter() {
//		if (this.isHistory) {
//			this.lv.setAdapter(this.addressWebAdapter);
//			this.addressWebAdapter.setList(this.userInfo.getAddresses());
//			this.addressWebAdapter.notifyDataSetChanged();
//			return;
//		}
//		this.lv.setAdapter(this.addressAdapter);
//		this.addressAdapter.setList(this.list);
//		this.addressAdapter.notifyDataSetChanged();
//	}
//
//	@Override
//	public void onClick(View v) {
//		switch (v.getId()) {
//		case R.id.ll_back /* 2131165223 */:
//			this.btn_address_back.performClick();
//		case R.id.btn_address_back /* 2131165224 */:
//			finish();
//		case R.id.img_voice1 /* 2131165226 */:
//			showIsrDialog();
//		case R.id.img_voice_clear /* 2131165227 */:
//			this.et_address.setText("");
//			this.img_voice1.setVisibility(0);
//			this.img_voice_clear.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
//		default:
//		}
//	}
//
//	private void showIsrDialog() {
//		this.recognizerDialog.setSampleRate(RATE.rate16k);
//		this.recognizerDialog.setEngine("poi", "vad_bos=4000,vad_eos=2000", null);
//		this.recognizerDialog.show();
//	}
//
//	public void onEnd(SpeechError error) {
//		if (!TextUtils.isEmpty(this.sb.toString())) {
//			this.et_address.setText(new StringBuilder(String.valueOf(this.et_address.getText().toString())).append(
//					this.sb.toString().replace("null", "")).toString());
//			String curTxt = this.et_address.getText().toString();
//			if (curTxt != null && curTxt.length() > 0) {
//				this.et_address.setSelection(curTxt.length());
//			}
//		}
//		if (this.sb != null) {
//			this.sb.setLength(0);
//			this.sb = null;
//		}
//	}
//
//	public void onResults(ArrayList<RecognizerResult> results, boolean isLast) {
//		if (this.sb == null) {
//			this.sb = new StringBuffer();
//		}
//		this.sb.append(((RecognizerResult) results.get(0)).text);
//	}
//
//	@Override
//	public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//		if (this.isHistory) {
//			Intent intent = new Intent();
//			Bundle bundle = new Bundle();
//			bundle.putBoolean("isquan", true);
//			bundle.putSerializable("address", this.userInfo.getAddresses().get(position));
//			intent.putExtras(bundle);
//			setResult(-1, intent);
//			finish();
//			return;
//		}
//		this.addressAdapter.setSelectItem(position);
//		this.addressAdapter.notifyDataSetInvalidated();
//		getLocation(new StringBuilder(String.valueOf(this.list.get(position).getCity()))
//				.append(this.list.get(position).getDistrict()).append(this.list.get(position).getName()).toString());
//	}
//
//	private void getLocation(String addressname) {
//		this.cusProDialog.show();
//		AddressMethod.getLocation(this, addressname, new SetAddress() {
//			@Override
//			public void onSuccess(String result) {
//				AddressSerchIndexActivity.this.cusProDialog.dismiss();
//				try {
//					JSONObject jsonObject2 = new JSONObject(result).getJSONObject("result").getJSONObject("location");
//					Intent intent = new Intent();
//					Bundle bundle = new Bundle();
//					bundle.putBoolean("isquan", false);
//					bundle.putString("position", jsonObject2.getString("lng") + "," + jsonObject2.getString("lat"));
//					intent.putExtras(bundle);
//					AddressSerchIndexActivity.this.setResult(-1, intent);
//					AddressSerchIndexActivity.this.finish();
//				} catch (JSONException e) {
//					e.printStackTrace();
//					MainToast.show(AddressSerchIndexActivity.this, AddressSerchIndexActivity.this.getResources()
//							.getString(R.string.app_error), 0);
//				}
//			}
//
//			@Override
//			public void onFail() {
//				AddressSerchIndexActivity.this.cusProDialog.dismiss();
//				MainToast.show(AddressSerchIndexActivity.this,
//						AddressSerchIndexActivity.this.getResources().getString(R.string.netFail), 0);
//			}
//		});
//	}
//
//	@Override
//	public boolean dispatchTouchEvent(MotionEvent ev) {
//		if (ev.getAction() == 0) {
//			View v = getCurrentFocus();
//			if (isShouldHideInput(v, ev)) {
//				hideSoftInput(v.getWindowToken());
//			}
//		}
//		return super.dispatchTouchEvent(ev);
//	}
//
//	private boolean isShouldHideInput(View v, MotionEvent event) {
//		if (v == null || !(v instanceof EditText)) {
//			return false;
//		}
//		int[] l = new int[2];
//		v.getLocationInWindow(l);
//		int left = l[0];
//		int top = l[1];
//		return event.getX() <= (left) || event.getX() >= (left + v.getWidth()) || event.getY() <= (top)
//				|| event.getY() >= (top + v.getHeight());
//	}
//
//	private void hideSoftInput(IBinder token) {
//		if (token != null) {
//			((InputMethodManager) getSystemService("input_method")).hideSoftInputFromWindow(token,
//					BitmapCacheManagementTask.MESSAGE_FLUSH);
//		}
//	}
// }
